codefandomcom-20200214-history
XML FAQ
Questions and answers In XSLT, why am I not getting the expected results from a select attribute? XSLT allows the select attribute on a number of instruction elements, such as xsl:apply-templates and xsl:value-of. This attribute works basically the same way on each of these elements: it locates a node-set using the XPath expression stored in the attribute, and the resulting node-set is provided to the instruction element for further processing. Your select attribute may not be selecting the node or nodes that you expect for one of several reasons. One common reason is that your XPath expression may refer to element names with no namespace prefix, but those elements are actually in a namespace. Please see the question on namespaces in XPath for more information. Another reason for incorrect select results may be that your context node is not what you expect. All XPath expressions are evaluated with respect to a context node."Introduction" section to the XPath 1 Recommendation The context node starts off as the document root (which can be selected from any context using the XPath expression /), and two XSLT instructions change the context node. First, a template defined with an xsl:template element that has a match attribute will change the context node to the node currently being matched by the match attribute. Consider the following template: In this example, the context node for the XPath expression ex:name will be the currently matched ex:person, so this expression will look for the child element of that ex:person element named ex:name. Note that calling a template by name with an instruction like does not change the context node. The only other way to change the context node is with the xsl:for-each element, which will change the context node for the children of the xsl:for-each element to the node currently being processed in the loop. In XSLT, why am I seeing output for an element with no matching template? XSLT provides a set of built-in template rules that can provide some useful default behavior."Built-in Template Rules" section of the XSLT 1 Recommendation The built-in rules will walk through the root node and any element nodes, and will output any text nodes that are found. This default behavior can be confusing if you don't expect it or don't know how to deal with it. (On the other hand, thinking about how this small set of rules works recursively over your input document can help you understand XSLT much better.) You can override any or all of these actions using explicit templates in your XSLT script. How do I deal with namespaces in XPath? XPath 1 requires you to use a namespace prefix to indicate the namespace for an element or attribute; a name test with no prefix matches an element or attribute that is not in a namespace."Node Tests" section of the XPath 1 Recommendation For example, the XPath expression /invoice/@version would match the version attribute on the root element of this document: ... additional content here But not of this document: ... additional content here To match the version attribute in the second document above, you would have to declare a prefix for the http://example.org/invoice/1.0 namespace and use that prefix in the XPath expression. In an XML host document, such as XSLT, this would look like xmlns:inv="http://example.org/invoice/1.0". (You can choose your own prefix instead of inv.) Then your XPath expression (with respect to this namespace declaration) would be /inv:invoice/@version. What namespace applies to my unprefixed attributes? Unlike elements, attributes without a namespace prefix have no namespace."Namespace Defaulting" section of the Namespaces in XML 1.0 Recommendation For example, in this document: ... additional content here The invoice element is in the http://example.org/invoice/1.0 namespace, but the version attribute is not in a namespace. This can be a source of confusion for those trying to query XML documents with XPath, such as when using XSLT or XQuery. References Category:XML